﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using POP.Pop3;
using EmailClient.DTO;
using System.IO;
using EmailClient.DAO;

namespace EmailClient
{
    public partial class AddAccount : Form
    {
        public static string pop3_host = "";
        public static int pop3_port = 0;
        public static string smtp_host = "";
        public static int smtp_port = 0;
        private List<string> ListAccount
        {
            get;
            set;
        }
        public AddAccount()
        {
            InitializeComponent();
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            if (!NewEmail.CheckEmail(txt_Email.Text.Trim()) || txt_Email.Text.Trim() == "")
                label_checkEmail.Visible = true;
            if (txt_Password.Text != txt_RePassword.Text || txt_Password.Text.Trim() == "")
                label_checkPassword.Visible = true;
            else
                if (NewEmail.CheckEmail(txt_Email.Text.Trim()) && txt_SmtpHost.Text != ""
                    && txt_SmtpPort.Text != "" && txt_Pop3Host.Text != "" && txt_Pop3Port.Text != "")
                {
                    {
                        smtp_host = txt_SmtpHost.Text;
                        smtp_port = Int32.Parse(txt_SmtpPort.Text);
                        pop3_host = txt_Pop3Host.Text;
                        pop3_port = Int32.Parse(txt_Pop3Port.Text);
                        if (ConnectServer(pop3_host, pop3_port, txt_Email.Text.Trim(), txt_Password.Text.Trim()))
                        {
                            try
                            {
                                searchSMTP(txt_Email.Text.Trim());
                                string accAddress = txt_Email.Text.Trim();
                                Account acc = new Account();
                                acc.AccountAddress = txt_Email.Text.Trim();
                                acc.Password = txt_Password.Text.Trim();
                                acc.SmtpHost = smtp_host;
                                acc.SmtpPort = smtp_port;
                                acc.Pop3Host = pop3_host;
                                acc.Pop3Port = pop3_port;
                                AccountDAO.AddAccount(acc);
                                string path_Inbox = @"EmailDB\" + accAddress + @"\Inbox";
                                if (!Directory.Exists(path_Inbox))
                                    Directory.CreateDirectory(path_Inbox);
                                string path_Spam = @"EmailDB\" + accAddress + @"\Spam";
                                if (!Directory.Exists(path_Spam))
                                    Directory.CreateDirectory(path_Spam);
                                string path_DeteledItems = @"EmailDB\" + accAddress + @"\Deteled Items";
                                if (!Directory.Exists(path_DeteledItems))
                                    Directory.CreateDirectory(path_DeteledItems);
                                string path_SentItems = @"EmailDB\" + accAddress + @"\Sent Items";
                                if (!Directory.Exists(path_SentItems))
                                    Directory.CreateDirectory(path_SentItems);
                                string path_Attachments = @"EmailDB\" + accAddress + @"\Attachments";
                                if (!Directory.Exists(path_Attachments))
                                    Directory.CreateDirectory(path_Attachments);

                                FormMain f = new FormMain();
                                //f.sender(accAddress);
                                f.AccountInbox_FormMain.Items.Add(accAddress);
                                f.AccountInbox_FormMain.ResetText();
                                f.ListAccount_FormMain.Items.Add(accAddress);
                                f.ListAccount_FormMain.ResetText();
                                //f.ShowDialog();
                                this.Close();
                                MessageBox.Show("Add account successfully!");
                                //f.SetDatasourceAccountInbox(ListAccount);      
                            }
                            catch
                            {
                                MessageBox.Show("Add account failed");
                            }
                        }
                        else
                            MessageBox.Show("Can't connect to server!");
                    }
                }
                else
                {
                    MessageBox.Show("Can't connect to server!");
                }
        }

        private void AddAccount_Load(object sender, EventArgs e)
        {
            label_checkPassword.Visible = false;
            label_checkEmail.Visible = false;
        }
        public void searchPOP3(string emailAddress)
        {
            pop3_host = null;
            pop3_port = 0;
            if (emailAddress.Contains("@gmail.com"))
            {
                pop3_host = "pop.gmail.com";
                pop3_port = 995;
            }
            if (emailAddress.Contains("@outlook.com") || emailAddress.Contains("@hotmail.com"))
            {
                pop3_host = "pop3.live.com";
                pop3_port = 995;
            }
            if (emailAddress.Contains("@yahoo.com"))
            {
                pop3_host = "pop.mail.yahoo.com";
                pop3_port = 995;
            }
        }
        public void searchSMTP(string emailAddress)
        {
            smtp_host = null;
            smtp_port = 0;
            if (emailAddress.Contains("@gmail.com"))
            {
                smtp_host = "smtp.gmail.com";
                smtp_port = 587;
            }
            if (emailAddress.Contains("@outlook.com") || emailAddress.Contains("@hotmail.com"))
            {
                smtp_host = "smtp.live.com";
                smtp_port = 587;
            }
            if (emailAddress.Contains("@yahoo.com"))
            {
                smtp_host = "smtp.mail.yahoo.com";
                smtp_port = 587;
            }
        }
        public bool ConnectServer(string pop3_host, int po3_port, string emailAddress, string password)
        {
            try
            {
                Pop3Client pop3 = new Pop3Client();
                pop3.Connect(pop3_host, po3_port, true);
                pop3.Authenticate(emailAddress, password);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void txt_Password_TextChanged(object sender, EventArgs e)
        {
            label_checkPassword.Visible = false;
        }

        private void txt_RePassword_TextChanged(object sender, EventArgs e)
        {
            label_checkPassword.Visible = false;
        }

        private void txt_Email_TextChanged(object sender, EventArgs e)
        {
            label_checkEmail.Visible = false;
            searchPOP3(txt_Email.Text.Trim());
            searchSMTP(txt_Email.Text.Trim());
            if (smtp_port != 0 && pop3_port != 0)
            {
                txt_SmtpHost.Text = smtp_host;
                txt_SmtpPort.Text = smtp_port.ToString();
                txt_Pop3Host.Text = pop3_host;
                txt_Pop3Port.Text = pop3_port.ToString();
            }
            else
            {
                txt_SmtpHost.Text = null;
                txt_SmtpPort.Text = null;
                txt_Pop3Host.Text = null;
                txt_Pop3Port.Text = null; 
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }   
}
